<script lang="ts" setup>
  import { useRouter, onBeforeRouteUpdate, useRoute } from 'vue-router';
  import { useUserStoreHook } from '/@/store/modules/user';
  const router = useRouter();
  onBeforeRouteUpdate(to => {
    if (!to.query.code) {
      userStore.resetToken();
      window.location.href = import.meta.env.VITE_APP_REDIRECT_AUTH;
      return;
    }
  });
  const userStore = useUserStoreHook();
  const route = useRoute();
  userStore.resetToken();

  if (route.query.code) {
    userStore.login(String(route.query.code)).then(res => {
      console.log(res, 'Login##');
      router.push('/').catch(err => {
        console.warn(err);
      });
    });
  } else {
    window.location.href = import.meta.env.VITE_APP_REDIRECT_AUTH;
  }
</script>

<template>
  <div class="login-container">
    <p class="login-wrapper">
      <el-icon class="is-loading"><Loading /></el-icon>
      加载中，请稍后...
    </p>
  </div>
</template>

<style lang="scss" scoped>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    min-height: 100%;

    .login-wrapper {
      display: flex;
      margin: 0 auto;
      color: var(--el-color-primary);
      user-select: none;
      align-items: center;
      justify-content: center;
    }
  }
</style>
